from rest_framework import viewsets
from rest_framework.pagination import PageNumberPagination
from rest_framework.decorators import action
from rest_framework.permissions import SAFE_METHODS
from rest_framework.response import Response

from .serializers import GetPreDepositLogSuperSerializer, GetPreDepositLogAllowSerializer, \
    PostPreDepositLogSuperSerializer, GetPreDepositLogDetailSerializer
from .permissionFilterBackends import PreDepositLogAllowPermissionFilterBackend
from .models import PreDepositLog
from rest_framework import permissions
from utils.decorators.responses import json_response, FlagException
from utils.decorators.auth import is_superuser, is_authenticated


class PreDepositLogsPagination(PageNumberPagination):
    page_size = 20
    page_query_param = "page"
    page_size_query_param = 'limit'

    # 最大能请求几条
    max_page_size = 100


class PreDepositLogViewSet(viewsets.mixins.ListModelMixin,
                           viewsets.mixins.CreateModelMixin,
                           viewsets.mixins.RetrieveModelMixin,
                           viewsets.GenericViewSet):
    pagination_class = PreDepositLogsPagination
    queryset = PreDepositLog.objects.all()
    serializer_class = PostPreDepositLogSuperSerializer
    permission_classes = [permissions.IsAuthenticatedOrReadOnly]
    filter_backends = [PreDepositLogAllowPermissionFilterBackend]

    @is_authenticated
    @json_response
    def list(self, request, *args, **kwargs):
        serializer = None
        query = PreDepositLog.objects.all()
        user_id = request.query_params.get('user', None)
        self.pagination_class = PreDepositLogsPagination
        if self.request.user.is_superuser:
            if user_id:
                query = query.filter(user__id=user_id)
        else:
            query = query.filter(user__id=request.user.id)

        queryset = self.paginate_queryset(query)
        serializer = GetPreDepositLogSuperSerializer(queryset, many=True)

        return self.get_paginated_response(serializer.data).data

    @is_authenticated
    @json_response
    def retrieve(self, request, *args, **kwargs):
        user_id = kwargs['pk']
        serializer = GetPreDepositLogDetailSerializer(data=request.data)
        serializer.is_valid()
        # user_id = serializer.data['user_id']
        query = PreDepositLog.objects.all()
        if self.request.user.is_superuser:
            if user_id:
                query = query.filter(user__id=user_id)
        else:
            query = query.filter(user__id=request.user.id)

        queryset = self.paginate_queryset(query)
        serializer = GetPreDepositLogSuperSerializer(queryset, many=True)
        return self.get_paginated_response(serializer.data).data

    @is_superuser
    @json_response
    def create(self, request, *args, **kwargs):
        serializer = PostPreDepositLogSuperSerializer(data=request.data)
        try:
            serializer.is_valid(raise_exception=True)
            serializer.save()
            return
        except Exception as e:
            raise FlagException(False, 400, e.__str__())
